package com.imooc.o2o.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.Principal;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class FileUtil {
	//系统的文件分隔符
	private static String seperator = System.getProperty("file.separator");
	private static final SimpleDateFormat sDateFormat = new SimpleDateFormat(
			"yyyyMMddHHmmss"); // 时间格式化的格式
	private static final Random r = new Random();
	public static String getImgBasePath() {
		String os = System.getProperty("os.name");
		String basePath = "";
		if (os.toLowerCase().startsWith("win")) {
			basePath = "F:/o2o/src/main/webapp/WEB-INF/images/";
		} else {
			basePath = "/home/xiangzepro/";
		}
		basePath = basePath.replace("/", seperator);
		return basePath;
	}

	public static String getHeadLineImagePath() {
		String headLineImagePath = "/upload/images/item/headtitle/";
		headLineImagePath = headLineImagePath.replace("/", seperator);
		return headLineImagePath;
	}

//	public static String getShopCategoryImagePath() {
//		String shopCategoryImagePath = "/upload/images/item/shopcategory/";
//		shopCategoryImagePath = shopCategoryImagePath.replace("/", seperator);
//		return shopCategoryImagePath;
//	}
	
	public static String getPersonInfoImagePath() {
		String personInfoImagePath = "/upload/images/item/personinfo/";
		personInfoImagePath = personInfoImagePath.replace("/", seperator);
		return personInfoImagePath;
	}

	public static String getShopImagePath(long shopId) {
		StringBuilder shopImagePathBuilder = new StringBuilder();
		shopImagePathBuilder.append("/upload/images/item/shopadmin/");
		shopImagePathBuilder.append(shopId);
		shopImagePathBuilder.append("/");
		String shopImagePath = shopImagePathBuilder.toString().replace("/",
				seperator);
		return shopImagePath;
	}
	public static String getShopCategoryImagePath(long shopCategoryId) {
		StringBuilder shopCategoryImagePathBuilder = new StringBuilder();
		shopCategoryImagePathBuilder.append("/upload/images/item/shopcategory/");
		shopCategoryImagePathBuilder.append(shopCategoryId);
		shopCategoryImagePathBuilder.append("/");
		String shopImageCategoryPath = shopCategoryImagePathBuilder.toString().replace("/",
				seperator);
		return shopImageCategoryPath;
	}

	public static String getRandomFileName() {
		// 生成随机文件名：当前年月日时分秒+五位随机数（为了在实际项目中防止文件同名而进行的处理）
		int rannum = (int) (r.nextDouble() * (99999 - 10000 + 1)) + 10000; // 获取随机的五位数
		String nowTimeStr = sDateFormat.format(new Date()); // 当前时间
		return nowTimeStr + rannum;
	}

	/**
	 *
	 * @param storePath
	 * StorePath是文件路径
	 */
	public static void deleteFile(String storePath) {
		File file = new File(getImgBasePath() + storePath);
		if (file.exists()) {
			//路径是目录,则将所有文件都删除
			if (file.isDirectory()) {
				File files[] = file.listFiles();
				for (int i = 0; i < files.length; i++) {
					files[i].delete();
				}
			}
			//路径是文件,删除该文件
			file.delete();
		}
	}
}
